using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

using Core;

namespace WorldEditor
{
    public partial class XNAPanel : UserControl, IServiceProvider
    {
        public PanelWithEvents BasePanel { get { return pnlEditor; } }
        public GraphicsDevice GraphicsDevice { get { return device; } }
        public SpriteBatch SpriteBatch { get { return spriteBatch; } }
        public ContentManager ContentManager { get { return contentManager; } }
        //Graphics Device
        protected GraphicsDevice device;
        //Timer for doing our Update/Draw Cycle
        protected Timer drawTimer;
        //Sprite Batch to draw sprites to drawable area
        protected SpriteBatch spriteBatch;

        private SimpleGraphicsDeviceService graphicsDeviceService;

        private ContentManager contentManager;

        public XNAPanel()
        {
            InitializeComponent();

            this.SizeChanged += new EventHandler(XNAPanel_SizeChanged);
            this.pnlEditor.KeyDown += new KeyEventHandler(XNAPanel_KeyDown);
            this.pnlEditor.KeyUp += new KeyEventHandler(XNAPanel_KeyUp);

            #region GraphicsDevice
            //Setup and create our graphics device
            PresentationParameters pp = new PresentationParameters();
            pp.BackBufferCount = 1;
            pp.IsFullScreen = false;
            pp.SwapEffect = SwapEffect.Discard;
            pp.BackBufferWidth = pnlEditor.Width;
            pp.BackBufferHeight = pnlEditor.Height;

            pp.AutoDepthStencilFormat = DepthFormat.Depth24Stencil8;
            pp.EnableAutoDepthStencil = true;
            pp.PresentationInterval = PresentInterval.Default;
            pp.BackBufferFormat = SurfaceFormat.Unknown;
            pp.MultiSampleType = MultiSampleType.None;
            //Create GraphicsDevice
            device = new GraphicsDevice(GraphicsAdapter.DefaultAdapter,
                DeviceType.Hardware, this.pnlEditor.Handle, pp);
            #endregion

            //Create GraphicsDeviceService
            graphicsDeviceService = new SimpleGraphicsDeviceService(device);
            //Create Content Manager
            contentManager = new ContentManager(this);
            //Create Spritebatch
            spriteBatch = new SpriteBatch(device);
            //Create timer and start it
            drawTimer = new Timer();
            drawTimer.Interval = 16;
            drawTimer.Tick += new EventHandler(drawTimer_Tick);
            drawTimer.Start();
        }

        protected void drawTimer_Tick(object sender, EventArgs e)
        {
            pnlEditor_Paint(null, null);
        }

        protected void pnlEditor_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            if (Enabled)
            {
                //Clear the screen
                device.Clear(Color.LightBlue);
                //Call the virtual draw function
                if (!this.DesignMode)
                    Draw();
                //Present the changes
                device.Present();
            }
        }

        protected void XNAPanel_SizeChanged(object sender, EventArgs e)
        {
            pnlEditor_SizeChanged(sender, e);
        }

        protected void pnlEditor_SizeChanged(object sender, System.EventArgs e)
        {
            drawTimer.Stop();

            if (pnlEditor.Width == 0 || pnlEditor.Height == 0)
                return;

            PresentationParameters pp = device.PresentationParameters;

            pp.BackBufferWidth = pnlEditor.Width;
            pp.BackBufferHeight = pnlEditor.Height;

            device.Reset(pp);

            drawTimer.Start();
        }

        protected virtual void pnlEditor_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            MouseMove(e);
        }

        protected void XNAPanel_KeyDown(object sender, KeyEventArgs e)
        {
            KeyPressDown(e);
        }

        protected void XNAPanel_KeyUp(object sender, KeyEventArgs e)
        {
            KeyPressUp(e);
        }

        protected virtual void Draw()
        {

        }

        protected virtual new void MouseMove(MouseEventArgs e)
        {
         
        }

        protected virtual void KeyPressDown(KeyEventArgs e)
        {
            
        }

        protected virtual void KeyPressUp(KeyEventArgs e)
        {

        }

        #region IServiceProvider Members

        public new object GetService(Type serviceType)
        {
            if (serviceType == typeof(Microsoft.Xna.Framework.Graphics.IGraphicsDeviceService))
                return this.graphicsDeviceService;
            else
                return base.GetService(serviceType);
        }

        #endregion

        
    }
}
